package com.hunk.core.auth.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hunk.core.auth.dto.UserRoleDTO;
import com.hunk.core.auth.entity.User;
import com.hunk.core.auth.entity.UserRole;
import com.hunk.core.auth.service.UserRoleService;
import com.hunk.core.auth.service.UserService;
import com.hunk.core.json.base.JsonResponse;
import com.hunk.core.web.model.JpaOmGridPage;
import com.hunk.core.web.model.JpaOmGridPage.JpaPageRequest;
import com.hunk.core.web.model.JpaOmGridPage.JpaPageResponse;
import com.hunk.utils.exception.CustomizeException;
import com.hunk.utils.io.Print;
@Controller
@RequestMapping("auth/user")
public class UserController {
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	UserService userService;
	
	@Autowired
	UserRoleService userRoleService;
	
	@RequestMapping("/page")	
	public @ResponseBody JpaPageResponse<User> getUserPage(HttpServletRequest request, 
			@RequestParam(required=false) Map<String, Object> params) {
		JpaOmGridPage.JpaPageRequest<User> page = new JpaPageRequest<User>(params);
		return userService.findPageByParams(page);
	}
	
	@RequestMapping("/save")	
	public @ResponseBody JsonResponse<User> saveUser(@Valid @ModelAttribute("user") User user, BindingResult bindingResult) {
		Print.println(user);
		JsonResponse<User> jr = new JsonResponse<User>("操作失败");
		if (bindingResult.hasErrors()) {
			StringBuilder sb = new StringBuilder();
			for (FieldError fe : bindingResult.getFieldErrors()) {
				sb.append(fe.getDefaultMessage()).append("<br>");
			}
			jr.setError(sb.toString());
			return jr;
		}
		
		userService.save(user);
		
		jr.setError("操作成功");
		jr.setResult(true);
		
		return jr;
	}
	
	@RequestMapping("/delete")	
	public @ResponseBody JsonResponse<User> deleteUser(@ModelAttribute("user") User user) {
		JsonResponse<User> jr = new JsonResponse<User>("操作失败");
		try {
			userService.delete(user);
			jr.setError("操作成功");
			jr.setResult(true); 
		} catch (CustomizeException e) {
			logger.warn(e);
		}
		return jr;
	}
	
	@RequestMapping("/saveUserRole")	
	public @ResponseBody JsonResponse<String> saveUserRole(@RequestBody UserRoleDTO userrole) {		
		userRoleService.save(userrole);
		JsonResponse<String> jr = new JsonResponse<String>("操作成功");			
		return jr;
	}
	
	@RequestMapping("/findUserRolesByUserId/{userid}")
	public @ResponseBody JsonResponse<List<UserRole>> findUserRoles(@PathVariable("userid") String userid) {		
		JsonResponse<List<UserRole>> jr = new JsonResponse<List<UserRole>>("操作成功");	
		jr.setResp(userRoleService.findUserRolesByUserid(userid));
		return jr;
	}
}
